/*
There are two sorted arrays nums1 and nums2 of
size m and n respectively. Find the median of
the two sorted arrays. The overall run time complexity should be O(log (m+n)).
*/
#include<stdio.h>

double findMedianSortedArrays(int *A, int m, int *B, int n) {
    int *arr;
    int num, i, j, k;
    num = m + n;
    i = 0;
    j = 0;
    k = 0;
    arr = (int *) malloc(sizeof(int) * num);
    while (i < m && j < n) {
        if (A[i] < B[j]) {
            arr[k++] = A[i++];
        } else {
            arr[k++] = B[j++];
        }
    }
    while (i < m) {
        arr[k++] = A[i++];
    }
    while (j < n) {
        arr[k++] = B[j++];
    }

    if (num & 0x1) {
        return arr[num / 2];
    } else {
        return (double) (arr[(num - 1) / 2] + arr[num / 2]) / 2;
    }
}

int main() {
    int A[] = {1, 2, 3, 7, 8, 12};
    int B[] = {4, 5, 6, 9, 10, 11};
    double median = findMedianSortedArrays(A, 6, B, 6);
    printf("%f\n", median);
    return 0;
}
